﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class master_MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetCustomerList();
        }
    }

    protected void GetCustomerList()
    {
        Customer cs = new Customer();
        DropDownList_Customer.DataSource = cs.GetAllCustomers().ToList();
        DropDownList_Customer.DataTextField = "CustomerName";
        DropDownList_Customer.DataValueField = "CustomerId";
        DropDownList_Customer.DataBind();
        DropDownList_Customer.Items.Insert(0, new ListItem("Select", "0"));
    }

    protected void Button_SearchCustomer_Click(object sender, EventArgs e)
    {
        string prefix = TextBox_Search.Text;
        Response.Redirect("../test/customerSearch.aspx?Par=" + UTIL.ReplaceSpace(prefix));
    }

    protected void Button_SearchService_Click(object sender, EventArgs e)
    {

    }

    protected void Button_SearchClient_Click(object sender, EventArgs e)
    {

    }

    protected void Button_CreateCustomer_Click(object sender, EventArgs e)
    {        
        string CustomerName = TextBox_CustomerName.Text;
        string CustomerCode = TextBox_CustomerCode.Text;
        Customer cs = new Customer(CustomerCode, CustomerName, "admin");
        Exception ex = new Exception();
        if (cs.Save(ref ex))
        {
            Response.Redirect("customerDetail.aspx?Id="+ cs.CustomerId.ToString());
        }        
    }

    protected void Button_SaveClient_Click(object sender, EventArgs e)
    {
        int CustomerId = Convert.ToInt32(DropDownList_Customer.SelectedValue);
        string ClientName = TextBox_ClientName.Text;
        string ClientCode = TextBox_ClientCode.Text;
        Client cl = new Client(ClientCode, ClientName, "admin", CustomerId);
        Exception ex = new Exception();
        if (cl.Save(ref ex))
        {
            Response.Redirect("clientDetail.aspx?Id=" + cl.ClientId.ToString());
        }
    }
}
